package cntv.player.core.util;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cntv.player.engine.Const;
import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.widget.Toast;

/**
 * 通用工具类
 * @author zengliang
 *
 */
public class Utils {  
	
	/**
	 * 获取系统SDK版本
	 * @return
	 */
	public static int systemVersions(){
		int currentapiVersion=android.os.Build.VERSION.SDK_INT;
		return currentapiVersion;
	}
	
	/**
	 * 系统版本是否大于或者等于14(即4.0版本)
	 * @return 
	 */
	public static boolean isHighVersion(){
//		return systemVersions() >= 16;
		return systemVersions() >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
	}
	
	/**
	 * 系统版本是否等于19(即4.4版本)
	 * @return 
	 */
	public static boolean isKitKatVersion(){
//		return systemVersions() == Build.VERSION_CODES.KITKAT;
		return systemVersions() == 19;
	}
	/**
	 * 系统版本是否等于14或者15(即4.0版本)
	 * @return
	 */
	public static boolean isICMVersion(){
		return systemVersions() == Build.VERSION_CODES.ICE_CREAM_SANDWICH 
				|| systemVersions() == Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
	}
	
	/**
	 * 判断网络是否连接
	 * @param context
	 * @return
	 */
	public static boolean isNetConneted(Context context){
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo= cm.getActiveNetworkInfo();
		return networkInfo != null && networkInfo.isConnectedOrConnecting();
	}
	
	
	/**
	 * 获取当前网络类型
	 * @param context
	 * @return -1 没有网络
	 */
	public static int getNetType(Context context) {
		int netType = -1; 
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo= cm.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
			netType = networkInfo.getType();
		}
		return netType;
	}
	
	
	/**
	 * 根据手机是否加载sd卡，配置对应缓存路径 .../CACHE_PATH_NAME
	 * @param context
	 * @return 
	 */
	public static String getCachePath(Context context){
		if(context == null){
			return null;
		}
		if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ){
			if(context.getExternalCacheDir() != null){
				File file = new File(context.getExternalCacheDir().getPath() + "/" + Const.CACHE_PATH_NAME);
				if(!file.exists()){
					file.mkdir();
				}
				return file.getPath(); //cachePath:  sdcard/Android/data/data/{packageName}/cache/cntvplay
			}
		}
		return context.getCacheDir().getPath();
	}
	
	/**
	 * 根据手机是否加载sd卡，配置对应视频缓存路径 
	 * @param context
	 * @return
	 */
	public static String getCacheVideoPath(String videoName) {
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
			File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ "/" + Const.CACHE_VIDEO_PATH_NAME + "/" + videoName);
			if (!file.exists()) {
				file.mkdirs(); 
			}
			return file.getPath(); // cachePath:
									// sdcard/Download/cntvvideo/"videoName"
		}
		return null;
	}
	
	/**
	 * 随机生成一个1到10000之间的随机数
	 * @return
	 */
	public static int generateRandom(){
		return (int)(Math.random()*10000 + 1);
	}
	
   /**
    * dip转px  
    * @param context
    * @param dpValue
    * @return
    */
    public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
  
    /**
     * px 转dip
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }
    
    /**
     * 获取手机系统density
     * @param context
     * @return
     */
    public static float density(Context context){
    	final float scale = context.getResources().getDisplayMetrics().density; 
    	return scale;
    }
   /**
    * 获取手机屏幕宽
    * @param context
    * @return
    */
    public static int screenWidth(Context context){
    	final int width = context.getResources().getDisplayMetrics().widthPixels; 
    	return width;
    }
    /**
     * 获取手机屏幕高
     * @param context
     * @return
     */
    public static int screenHeight(Context context){
    	final int height = context.getResources().getDisplayMetrics().heightPixels; 
    	return height;
    }
    
    
	/**
	 * 获取状态栏高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getStatusBarHeight(Activity context) {
		Rect frame = new Rect();
		context.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		return statusBarHeight;
	}

	/**
	 * 显示Toast
	 * @param context
	 * @param string
	 */
	public static void displayToast(Context context, String string) {
		Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 得到所画字符串的宽度和高度
	 * @param paint 画笔
	 * @param string 需要获得高度和宽度的字符串
	 * @return int[] 
	 * 		int[0] 宽
	 * 		int[1] 高
	 */
	public static int[] getStringBounds(Paint paint,String string){
		int[] data = new int[2]; 
		Rect bounds = new Rect();	
		paint.getTextBounds(string,0,string.length(),bounds);
		data[0] = bounds.width();
		data[1] = bounds.height();
		return data;
	}
	
	/**
	 * 字符串转long类型
	 * @param str
	 * @return
	 */
	public static long strToLong(String str){
		if(str == null || "".equals(str.trim())){
			return 0;
		}
		try {
			return Long.parseLong(str);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 字符串转Int类型
	 * @param str
	 * @return
	 */
	public static int strToInt(String str){
		if(str == null || "".equals(str.trim())){
			return 0;
		}
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 将字符串中的数字过滤出来
	 * @param original
	 * @return
	 */
	public static int filterNum(String original) {
		Pattern p = Pattern.compile("[^0-9]");
		Matcher matcher = p.matcher(original); //匹配出非数字字符
		String num = matcher.replaceAll("").trim();//替换成空字符
		if(num == null || "".equals(num)){
			return 0;
		}else{
			return Integer.parseInt(num);
		}
	}
}